package combat5;

import java.util.Scanner;

import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.CannotProceed;
import org.omg.CosNaming.NamingContextPackage.NotFound;

import ca.etsmtl.log720.lab1.BanqueDossiers;
import ca.etsmtl.log720.lab1.BanqueDossiersHelper;
import ca.etsmtl.log720.lab1.BanqueInfractions;
import ca.etsmtl.log720.lab1.BanqueInfractionsHelper;
import ca.etsmtl.log720.lab1.CollectionDossier;
import ca.etsmtl.log720.lab1.CollectionInfraction;
import ca.etsmtl.log720.lab1.NiveauHorsBornesException;
import ca.etsmtl.log720.lab1.NoPermisExisteDejaException;

//create by martin souliere
public class Client {
	public static void main(String args[]) throws InvalidName, NotFound, CannotProceed, org.omg.CosNaming.NamingContextPackage.InvalidName {

		org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, null);

		// get hold of the naming service
		NamingContextExt nc = NamingContextExtHelper.narrow(orb.resolve_initial_references("NameService"));

		Scanner inputReader = new Scanner(System.in);
		while (true) {
			showOptions();
			int number;
			try {
				number = inputReader.nextInt();
			} catch (Exception e) {
				number = 0;
			}
			if (number == 0)
				break;
			switch (number) {
			case 1:
				ajouterDossier(inputReader,nc);
				break;
			case 2:
				ajouterInfraction(inputReader,nc);
				break;
			case 3:
				voirDossiers(nc);
				break;
			case 4:
				voirInfraction(nc);
				break;
			}
		}
	}

	private static void ajouterDossier(Scanner inputReader, NamingContextExt nc)
			throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {

		NameComponent[] nameBanqueDossiers = new NameComponent[] { new NameComponent("BanqueDossiers", "service") };
		// resolve name to get a reference to our server
		BanqueDossiers banqueDossiers = BanqueDossiersHelper.narrow(nc.resolve(nameBanqueDossiers));

		System.out.println("Veuillez entrer  le nom:");
		String nom = inputReader.next();
		System.out.println("Veuillez entrer  le prenom:");
		String prenom = inputReader.next();
		System.out.println("Veuillez entrer  le numero de permis:");
		String noPermis = inputReader.next();
		System.out.println("Veuillez entrer  le numero de plaque:");
		String noPlaque = inputReader.next();
		while (true) {
			try {
				banqueDossiers.ajouterDossier(nom, prenom, noPermis, noPlaque);
				break;

			} catch (NoPermisExisteDejaException e) {
				System.out.println("le numero de permis existe deja");
				System.out
						.println("Veuillez entrer le numero de permis a nouveau:");
				System.out.println("Pour annuler faite le 0");
				noPermis = inputReader.next();
				if (noPlaque.equals("0")) {
					return;
				}
			}
		}
	}

	private static void ajouterInfraction(Scanner inputReader,
			NamingContextExt nc) throws NotFound, CannotProceed,
			org.omg.CosNaming.NamingContextPackage.InvalidName {
		NameComponent[] nameBanqueInfractions = new NameComponent[] { new NameComponent("BanqueInfractions", "service") };
		// resolve name to get a reference to our server
		BanqueInfractions banqueInfractions = BanqueInfractionsHelper.narrow(nc
				.resolve(nameBanqueInfractions));

		System.out.println("Veuillez entrer la description:");
		String description = inputReader.next();
		System.out.println("Veuillez entrer le niveau entre 1 et 10:");
		
		int niveau = 0;
		try {
			niveau = inputReader.nextInt();
		} catch (Exception e2) {
			System.out.println("valeur invalide");
			return;
		}	
		while (true) {
			try {
				banqueInfractions.ajouterInfraction(description, niveau);
				break;
			} catch (NiveauHorsBornesException e) {
				System.out.println("le niveau doit etre entre 1 et 10");
				System.out.println("Veuillez entrer le niveau a nouveau:");
				System.out.println("Pour annuler faite le 0");
				try {
					niveau = inputReader.nextInt();
				} catch (Exception e2) {
					System.out.println("valeur invalide");
					break;
				}		
				if (niveau == 0) {
					break;
				}
			}
		}
	}

	private static void voirDossiers(NamingContextExt nc) throws NotFound, CannotProceed, org.omg.CosNaming.NamingContextPackage.InvalidName {
		NameComponent[] nameBanqueDossiers = new NameComponent[] { new NameComponent("BanqueDossiers", "service") };
		// resolve name to get a reference to our server
		BanqueDossiers banqueDossiers = BanqueDossiersHelper.narrow(nc.resolve(nameBanqueDossiers));
		CollectionDossier dossiers = banqueDossiers.dossiers();
		System.out.println("DOSSIERS---------------------");
		for (int i = 0; i < dossiers.size(); i++) {
			System.out.println(dossiers.getDossier(i)._toString());
		}
		System.out.println("---------------------");
	}

	private static void voirInfraction(NamingContextExt nc) throws NotFound, CannotProceed, org.omg.CosNaming.NamingContextPackage.InvalidName {
		NameComponent[] nameBanqueInfractions = new NameComponent[] { new NameComponent("BanqueInfractions", "service") };
		BanqueInfractions banqueInfractions = BanqueInfractionsHelper.narrow(nc.resolve(nameBanqueInfractions));
		CollectionInfraction infractions = banqueInfractions.infractions();
		System.out.println("INFRACTIONS---------------------");
		for (int i = 0; i < infractions.size(); i++) {
			System.out.println(infractions.getInfraction(i)._toString());
		}
		System.out.println("---------------------");
	}

	private static void showOptions() {
		System.out.println("Please enter input from command prompt: ");
		System.out.println("0-Fermer l'application");
		System.out.println("1-Ajouter un dossier");
		System.out.println("2-Ajouter une infraction");
		System.out.println("3-Visualiser les dossiers");
		System.out.println("4-Visualiser les infractions");
	}
}
